﻿
function checkRequired(theForm, idTd ,textAlert)
{
    if (theForm !=''&&spaceCheck(theForm)!=true)
    {
        document.getElementById(idTd).innerHTML="";
        return true;
    }
    else
    {
        document.getElementById(idTd).innerHTML=textAlert;
        return false;
    }
}

function checkUploadPic1(theForm, idTd ,textAlert)
{
    if(theForm!='')
	{
	    ext = theForm.substring(theForm.lastIndexOf('.') + 1).toLowerCase(); 
		if(ext=="gif" || ext=="jpg" || ext=="jpeg" || ext=="png")
		{
			document.getElementById(idTd).innerHTML="";
			return true;
		}
		else
		{
			document.getElementById(idTd).innerHTML=textAlert;
			return false;	
		}
	}
	else
	{
	    return true;
	}
}

function uploadPicRequired(theForm, idTd ,textAlert)
{
    if(theForm!='')
	{
	    ext = theForm.substring(theForm.lastIndexOf('.') + 1).toLowerCase(); 
		if((ext=="gif" || ext=="jpg" || ext=="jpeg" || ext=="png") && (theForm !=''&&spaceCheck(theForm)!=true))
		{
			document.getElementById(idTd).innerHTML="";
			return true;
		}
		else
		{
			document.getElementById(idTd).innerHTML=textAlert;
			return false;	
		}
	}
	else
	{
	    document.getElementById(idTd).innerHTML=textAlert;
		return false;
	}
}

function checkUploadPicSwf(theForm, idTd ,textAlert)
{
    if(theForm!='')
	{
	    ext = theForm.substring(theForm.lastIndexOf('.') + 1).toLowerCase(); 
		if(ext=="gif" || ext=="jpg" || ext=="jpeg" || ext=="png" || ext=="swf")
		{
			document.getElementById(idTd).innerHTML="";
			return true;
		}
		else
		{
			document.getElementById(idTd).innerHTML=textAlert;
			return false;	
		}
	}
	else
	{
	    return true;
	}
}

function uploadPicSwfRequired(theForm, idTd ,textAlert)
{
    if(theForm!='')
	{
	    ext = theForm.substring(theForm.lastIndexOf('.') + 1).toLowerCase(); 
		if((ext=="gif" || ext=="jpg" || ext=="jpeg" || ext=="png" || ext=="swf") && (theForm !=''&&spaceCheck(theForm)!=true))
		{
			document.getElementById(idTd).innerHTML="";
			return true;
		}
		else
		{
			document.getElementById(idTd).innerHTML=textAlert;
			return false;	
		}
	}
	else
	{
	    document.getElementById(idTd).innerHTML=textAlert;
		return false;
	}
}

function checkLength(theForm, theLength, idTd ,textAlert)
{
    if (theForm.length > theLength)
    {
        document.getElementById(idTd).innerHTML=textAlert;
        return false;
    }
    else
    {
        document.getElementById(idTd).innerHTML="";
        return true;
    }
}

function checkLength2(theForm,theLength, idTd)
{
    if (theForm.value.length > theLength)
    {
        document.getElementById(idTd).innerHTML="<strong>Text to long</strong>";
		theForm.focus();
        return false;
    }
    else
    {
        document.getElementById(idTd).innerHTML="";
        return true;
    }
}

function checkLengthMin(theForm, theLength, idTd ,textAlert)
{
    if (theForm.length < theLength)
    {
        document.getElementById(idTd).innerHTML=textAlert;
        return false;
    }
    else
    {
        document.getElementById(idTd).innerHTML="";
        return true;
    }
}

function checkLengthMinMax(theForm, minLength, maxLength, idTd, textAlert) {
    if (theForm.length < minLength || theForm.length > maxLength) {
        document.getElementById(idTd).innerHTML = textAlert;
        return false;
    }
    else {
        document.getElementById(idTd).innerHTML = "";
        return true;
    }
}

//<input type="text" value="0" maxlength="10" id="phone_number" onkeypress="return numbersonly(event)" onkeyup="checkNum(this, 'testCheck')" onchange="checkNum(this, 'testCheck')" />
//<div id=testCheck></div>    
function numbersonly(e) 
{
    var unicode = e.charCode ? e.charCode : e.keyCode
    if (unicode != 8) { //if the key isn't the backspace key (which we should allow)
        if (unicode < 48 || unicode > 57)
        { //if not a number
            return false //disable key press
        }
    }
}
    
function checkNum(theForm,idTd)
{
	if(isNaN(theForm.value) && spaceCheck(theForm.value)!=true)
	{	
		theForm.value=1;
		document.getElementById(""+idTd+"").innerHTML="<strong>Numeric only</strong>";
		theForm.focus();
		return false;
	}
	else if(spaceCheck(theForm.value)==true)
	{
		theForm.value=1;
		document.getElementById(""+idTd+"").innerHTML="<strong>Numeric only!</strong>";
		theForm.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function checkNumOrEmpty(theForm,idTd)
{
    if(theForm.value.length == 0)
    {
        return true;
    }
    else
    {
	    if(isNaN(theForm.value) && spaceCheck(theForm.value)!=true)
	    {	
		    theForm.value=1;
		    document.getElementById(""+idTd+"").innerHTML="<strong>Numeric only</strong>";
		    theForm.focus();
		    return false;
	    }
	    else if(spaceCheck(theForm.value)==true)
	    {
		    theForm.value=1;
		    document.getElementById(""+idTd+"").innerHTML="<strong>Numeric only!</strong>";
		    theForm.focus();
		    return false;
	    }
	    else
	    {
		    return true;
	    }
	}
}

//allow decimal number(with point)
function checkNum2(theForm, idTd, textAlert)
{
	if(isNaN(theForm) && spaceCheck(theForm)!=true)
	{	
		document.getElementById(idTd).innerHTML=textAlert;
		return false;
	}
	else
	{
	    document.getElementById(idTd).innerHTML="";
		return true;
	}
}
function checkNum2must(theForm, idTd, textAlert)
{
	if(isNaN(theForm) && spaceCheck(theForm)!=true)
	{	
		document.getElementById(idTd).innerHTML=textAlert;
		return false;
	}
	else if(spaceCheck(theForm)==true)
	{
		document.getElementById(idTd).innerHTML=textAlert;
		return false;
	}
	else
	{
	    document.getElementById(idTd).innerHTML="";
		return true;
	}
}

//not allow decimal number(with point)
function checkNum3(theForm, idTd, textAlert)
{
	if(isNaN(theForm) && spaceCheck(theForm)!=true)
	{
		document.getElementById(idTd).innerHTML=textAlert;
		return false;
	}
	else if(theForm.length > 0 && theForm.toString().indexOf(".") != -1)
	{
		document.getElementById(idTd).innerHTML=textAlert;
		return false;
	}
	else
	{
	    document.getElementById(idTd).innerHTML="";
		return true;
	}
}
function checkNum3must(theForm, idTd, textAlert)
{
	if(isNaN(theForm) && spaceCheck(theForm)!=true)
	{
		document.getElementById(idTd).innerHTML=textAlert;
		return false;
	}
	else if(spaceCheck(theForm)==true)
	{
		document.getElementById(idTd).innerHTML=textAlert;
		return false;
	}
	else if(theForm.length > 0 && theForm.toString().indexOf(".") != -1)
	{
		document.getElementById(idTd).innerHTML=textAlert;
		return false;
	}
	else
	{
	    document.getElementById(idTd).innerHTML="";
		return true;
	}
}

//http://www.webcheatsheet.com/javascript/form_validation.php
function checkNumOrLetersOnly(theForm, idTd, textAlert) {
    var illegalChars = /[\W_]/; // allow only letters and numbers
    if (illegalChars.test(theForm)) {
        document.getElementById(idTd).innerHTML = textAlert;
        return false;
    }
    else {
        document.getElementById(idTd).innerHTML = "";
        return true;
    }
}

function checkEnglishSpacePointOnly(theForm, idTd, textAlert) {
    var illegalChars = /^[A-Za-z .]*$/; // allow only English letters and space
    if (!illegalChars.test(theForm)) {
        document.getElementById(idTd).innerHTML = textAlert;
        return false;
    }
    else {
        document.getElementById(idTd).innerHTML = "";
        return true;
    }
}

function checkSelectList(theForm, idTd, badValue, textAlert)
{
    if(theForm.options[theForm.selectedIndex].value==badValue)
    {
        document.getElementById(idTd).innerHTML=textAlert;
        return false;
    }
    else
    {
        document.getElementById(idTd).innerHTML="";
        return true;
    }
}

function checkCheckbox(theForm, idTd, textAlert)
{
    if(theForm.checked != true)
    {
        document.getElementById(idTd).innerHTML=textAlert;
        return false;
    }
    else
    {
        document.getElementById(idTd).innerHTML="";
        return true;
    }
}

function checkRadioButton(theForm, idTd, textAlert) 
{
    isChecked = false;
    for(i = 0; i < theForm.length; i++)
    { 
        if(theForm[i].checked){ isChecked = true; } 
    }
    if(isChecked == false)
    {
        document.getElementById(idTd).innerHTML=textAlert;
        return false;
    }
    else
    {
        document.getElementById(idTd).innerHTML="";
        return true;
    }
}
        
function checkOptionalEmail(theForm, idTd ,textAlert)
{
    if (theForm !=''&&spaceCheck(theForm)!=true)
    {
        if(emailCheck(theForm))
        {
            document.getElementById(idTd).innerHTML="";
            return true;
        }
        else
        {
            document.getElementById(idTd).innerHTML=textAlert;
            return false;
        }  
    }
    else
    {
        document.getElementById(idTd).innerHTML="";
        return true;
    }
}

function checkRequiredEmail(theForm, idTd ,textAlert)
{
    if (theForm !=''&&spaceCheck(theForm)!=true)
    {
        if(emailCheck(theForm))
        {
            document.getElementById(idTd).innerHTML="";
            return true;
        }
        else
        {
            document.getElementById(idTd).innerHTML=textAlert;
            return false;
        }  
    }
    else
    {
        document.getElementById(idTd).innerHTML=textAlert;
        return false;
    }
}

function checkEqual(theForm1, theForm2, idTd ,textAlert)
{
    if (theForm1 == theForm2)
    {
        document.getElementById(idTd).innerHTML="";
        return true;
    }
    else
    {
        document.getElementById(idTd).innerHTML=textAlert;
        return false;
    }
}

function checkNumMaxAmount(theForm, idTd, maxAmount ,textAlert)
{
    if (parseInt(theForm) <= maxAmount)
    {
        document.getElementById(idTd).innerHTML="";
        return true;
    }
    else
    {
        document.getElementById(idTd).innerHTML=textAlert;
        return false;
    }
}

function spaceCheck(theStr)
{
	str = theStr;
	strarray = str.split(" ");
	if(str.length==strarray.length-1){return theStr=true;}
}
function emailCheck(Email)
{
	if (Email.length >0)
	{
		var i   = Email.indexOf("@")
		var j   = Email.indexOf(".",i)
		var k   = Email.indexOf(",")
		var kk  = Email.indexOf(" ")
		var aa  = Email.indexOf("'")
		var jj  = Email.lastIndexOf(".") + 1
		var len = Email.length

		if ((i > 0) && (j > (i + 1)) && (k == -1) && (kk == -1) && (aa == -1) && (len - jj >= 2) && (len - jj <= 3)) 
		{
		 	return true;
		}
        else
		{
        	return false; 
		}	
	}
	else
	{
		 return false;
	}
}

var slicer1;
function makeValue(theForm,textLength,theCheck,urlPic)
{
	if (theForm.value.length > textLength)
	{
		document.getElementById(theCheck).innerHTML="הטקסט ערוך מידי<img src="+urlPic+" width=16 height=15 border=0>";
		slicer1=theForm.value.slice(0,textLength-1)
		theForm.value = slicer1;
		theForm.focus();
		return false;
	}
	else
	{
		document.getElementById(theCheck).innerHTML="";
	}
}

// Replaces all instances of the given substring.
String.prototype.replaceAll = function( 
	strTarget, // The substring you want to replace
	strSubString // The string you want to replace in.
	){
	var strText = this;
	var intIndexOfMatch = strText.indexOf( strTarget );
	 
	// Keep looping while an instance of the target string
	// still exists in the string.
	while (intIndexOfMatch != -1){
		// Relace out the current instance.
		strText = strText.replace( strTarget, strSubString )
		 
		// Get the index of any next matching substring.
		intIndexOfMatch = strText.indexOf( strTarget );
	}
	return( strText );
}
