// JavaScript Document


function validation_required(val){
    if(val == ""){return false}else{return true}
}
function validation_numeric(val){
    if(isNaN(val)){return false}else{return true}
}
function validation_greater_than_zero(val){
    if(val <= 0){return false}else{return true}
}
function validation_email(val){
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test(val) == false) {return false;}else{return true;}
}
function validation_date(val){
    if(val == ""){return true};
    var reg = /(?:0[1-9]|[12][0-9]|3[01])\-(?:0[1-9]|1[0-2])\-(?:20\d{2})/;
    if(reg.test(val) == false) {return false;}else{return true;}
}

/* ********************************************** */

function cleardata(tObj){
    if(document.getElementById(tObj).innerHTML != ""){
    document.getElementById(tObj).innerHTML = "";
    }
}
function appenddata(tObj, tVal){
    if(tVal != ""){
        document.getElementById(tObj).innerHTML = document.getElementById(tObj).innerHTML + tVal + '<br/>';
    }
}

function checkfield(tField, tMessage, tAppendTo, validation){

    bRet = true;
    //var validation = "required valid-email numeric greater-than-zero";
    avalidation = validation.split(" ")
    var myinput = document.getElementById(tField).value
    for(var i = 0; i < avalidation.length; i++){
        var svalidation = avalidation[i];
        if(svalidation == "required"){
            if(!validation_required(myinput)){
                bRet = false;
                break;
            }
        }
        if(svalidation == "valid-email"){
            if(!validation_email(myinput)){
                bRet = false;
                break;
            }
        }
        if(svalidation == "numeric"){
            if(!validation_numeric(myinput)){
                bRet = false;
                break;
            }
        }
        if(svalidation == "greater-than-zero"){
            if(!validation_greater_than_zero(myinput)){
                bRet = false;
                break;
            }
        }
        if(svalidation == "valid-date"){
            //alert(validation_date(myinput))
            if(!validation_date(myinput)){
                bRet = false;
                break;
            }
        }
    }

 if(!bRet){
  appenddata(tAppendTo, tMessage)
  return 'false';
 }else{
  return 'true'
 }
}
function checkfieldnomatch(tField, tMatch, tMessage, tAppendTo){
 if(document.getElementById(tField).value == tMatch){
  appenddata(tAppendTo, tMessage)
  return 'false';
 }else{
  return 'true'
 }
}
function checkfieldcompare(tField, tMatch, tMessage, tAppendTo){
 if(document.getElementById(tField).value != document.getElementById(tMatch).value){
  appenddata(tAppendTo, tMessage)
  return 'false';
 }else{
  return 'true'
 }
}

function checkdropdown(tField, tMessage, tAppendTo){
 if(document.getElementById(tField).selectedIndex == 0){
  //alert(tMessage)
  //document.getElementById(tField).focus()
  appenddata(tAppendTo, tMessage)
  return 'false';
 }else{
  return 'true'
 }
}



function makeslug(tSource, tDest){
	if(tSource.value != "" && document.getElementById(tDest).value == ""){
		tVal = tSource.value;
		tVal = tVal.toLowerCase();
		while(tVal.indexOf(" ") >= 0){tVal = tVal.replace(" ", "-");}
		while(tVal.indexOf("&") >= 0){tVal = tVal.replace("&", "and");}
		while(tVal.indexOf("/") >= 0){tVal = tVal.replace("/", "-");}
		while(tVal.indexOf(".") >= 0){tVal = tVal.replace(".", "");}
		while(tVal.indexOf(",") >= 0){tVal = tVal.replace(",", "");}
		while(tVal.indexOf("'") >= 0){tVal = tVal.replace("'", "");}
		while(tVal.indexOf("(") >= 0){tVal = tVal.replace("(", "");}
		while(tVal.indexOf(")") >= 0){tVal = tVal.replace(")", "");}
		while(tVal.indexOf("--") >= 0){tVal = tVal.replace("--", "-");}
		while(tVal.indexOf("*") >= 0){tVal = tVal.replace("*", "");}
		document.getElementById(tDest).value = tVal;
	}
}

function show(tObj){
    document.getElementById(tObj).style.display="inline";
}
function focusme(tObj, sVal){
    if(tObj.value == sVal){tObj.value = ""}
}
function blurme(tObj, sVal){
    if(tObj.value == ""){tObj.value = sVal}
}

