// JavaScript Document
/* null checking */
function check_fields(form_name,f_name,d_name,c_array)
{
 
    var incre=0;
    for (incre=0; incre<c_array; incre++)
    {
        var frm="document."+form_name+"."+f_name[incre]+".value";
        var foc="document."+form_name+"."+f_name[incre];
		
        if(trimSpaces(eval(frm))=="")
        {

            alert(d_name[incre]+appendString);
            var foc_field=eval(foc);
            foc_field.focus();
            return false;
        }

        
		
        if(f_name[incre]=='txtPassword')
        {
            var password               = "document."+form_name+".txtPassword.value";
            var confirmPassword        = "document."+form_name+".txtConfirmPassword.value";
                            
            if(eval(password) != '' &&  eval(confirmPassword) != '' )
            {
                if(eval(password) != eval(confirmPassword))
                {
                    alert("Password Mismatch !!!");
                    var foc_field=eval(foc);
                    foc_field.focus();
                    return false;
                }
            }
        }
       /* if(f_name[incre]=='txtFirstName')
        {
            var firstname			   = "document."+form_name+".txtFirstName.value";
            if(!(containsAlphabets(eval(firstname))))
            {
                alert("Enter a valid firstname");
                var foc_field=eval(foc);
                foc_field.focus();
                return false;
            }
        }
        if(f_name[incre]=='txtLastName')
        {
            var lastName			   = "document."+form_name+".txtLastName.value";
            if(!(containsAlphabets(eval(lastName))))
            {
                alert("Enter a valid lastname");
                var foc_field=eval(foc);
                foc_field.focus();
                return false;
            }
        }*/
		
        if(f_name[incre]=='txtUserName' ||  f_name[incre]=='txtPassword' || f_name[incre]=='txtZIP' )
        {

            /*if (special(eval(frm)) == true)
            {
                alert(d_name[incre]+msgHasSpecialCharacters);
                var foc_field=eval(foc);
                foc_field.focus();
                return false;
            }*/
        }
    /*    if(f_name[incre]=='txtZIP')
        {
            var zipcode				   = "document."+form_name+".txtZIP.value";
            var country = "document."+form_name+".bill_country.value";
           if(eval(country) == "US") {
                if(eval(zipcode)!='' && validateZIP(eval(zipcode)))
                {
                    alert("Enter valid zip code");
                    return false;
                }
            } 
        }*/
		
        //var txtCountry	   = document.getElementById("billCountrySelect").value;
        if(f_name[incre]=='txtPhone')
        {
            var Phone					="document."+form_name+".txtPhone.value";
            var regexObj = /^[0-9-(+)]+$/; //^[a-zA-z]+$/
            var phone=trimSpaces(eval(Phone));
			
            if (!regexObj.test(phone))
            {
                alert("Invalid Phone number");
                return false;
            }
        /*if (regexObj.test(phone)) {
					var formattedPhoneNumber =
					phone.replace(regexObj, "($1) $2-$3");
			} 
			else {
				alert("Invalid Phone number");
				return false;
			}*/
        }
    }
	
    return true;
}

function check_login(form_name,f_name,d_name,c_array)
{
    
    var incre=0;
        
    for (incre=0; incre<c_array; incre++)
    {
        var frm="document."+form_name+"."+f_name[incre]+".value";
        var foc="document."+form_name+"."+f_name[incre];
        var password               = "document."+form_name+".txtPassword.value";
        var confirmPassword        = "document."+form_name+".txtConfirmPassword.value";
				
   				
               
        if(trimSpaces(eval(frm))=="")
        {
            alert(d_name[incre]+appendString);
            var foc_field=eval(foc);
            foc_field.focus();
            return false;
        }
		
		
		
       /* if (special(eval(frm)) == true)
        {
            alert(d_name[incre]+msgHasSpecialCharacters);
            var foc_field=eval(foc);
            foc_field.focus();
            return false;
        }*/
                
    }
    return true;
}

function trimSpaces(stringValue) 
{
    // Checks the first occurance of spaces and removes them
    for(i = 0; i < stringValue.length; i++)
    {
        if(stringValue.charAt(i) != " ")
        {
            break;
        }
    }
	
    if(i > 0)
    {
        stringValue = stringValue.substring(i);
    }
	
    // Checks the last occurance of spaces and removes them
    strLength = stringValue.length - 1;
    for(i = strLength; i >= 0; i--)
    {
        if(stringValue.charAt(i) != " ")
        {
            break;
        }
    }
	
    if(i < strLength)
    {
        stringValue = stringValue.substring(0, i + 1);
    }
	
    // Returns the string after removing leading and trailing spaces.
    return stringValue;
}

function validateZIP(field) {
    var valid = "0123456789-";
    var hyphencount = 0;
   // return true;
  
    if (field.length!=5 && field.length!=10) {
        //alert("Please enter your 5 digit or 5 digit+4 zip code.");
        return true;
    }
    for (var i=0; i < field.length; i++) {
        temp = "" + field.substring(i, i+1);
        if (temp == "-") hyphencount++;
        if (valid.indexOf(temp) == "-1") {
            //alert("Invalid characters in your zip code.  Please try again.");
            return true;
        }
        if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) { 
            //alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
            return true;
        }
    }

}


function checkEmail(emailString)
{
    splitVal = emailString.split('@');
	
    if(splitVal.length <= 1)
    {
        alert("Please Enter A Valid Email Address !!");
        return false;
    }
    if(splitVal[0].length <= 0 || splitVal[1].length <= 0)
    {
        alert("Please Enter A Valid Email Address !!");
        return false;
    }
	
    splitDomain = splitVal[1].split('.');
    if(splitDomain.length <= 1)
    {
        alert("Please Enter A Valid Email Address !!");
        return false;
    }
	
    if(splitDomain[0].length <= 0 || splitDomain[1].length <= 1)
    {
        alert("Please Enter A Valid Email Address !!");
        return false;
    }
    return true;
}

function special(strString) 
{
    var iChars = "!#$%^&*()+=-[]\\\';,/{}|\":<>?~";
    var data =strString;
    for (var i = 0; i < data.length; i++) {
        if (iChars.indexOf(data.charAt(i)) != -1) {
            //alert ("Your string has special characters. \nThese are not allowed.");
            return true;
        }
    }
}

function checkMail(email)
{
    var str1=email;
    var arr=str1.split('@');
    var eFlag=true;
    if(arr.length != 2)
    {
        eFlag = false;
    }
    else if(arr[0].length <= 0 || arr[0].indexOf(' ') != -1 || arr[0].indexOf("'") != -1 || arr[0].indexOf('"') != -1 || arr[1].indexOf('.') == -1)
    {
        eFlag = false;
    }
    else
    {
        var dot=arr[1].split('.');
        if(dot.length < 2)
        {
            eFlag = false;
        }
        else
        {
            if(dot[0].length <= 0 || dot[0].indexOf(' ') != -1 || dot[0].indexOf('"') != -1 || dot[0].indexOf("'") != -1)
            {
                eFlag = false;
            }

            for(i=1;i < dot.length;i++)
            {
                if(dot[i].length <= 0 || dot[i].indexOf(' ') != -1 || dot[i].indexOf('"') != -1 || dot[i].indexOf("'") != -1 || dot[i].length > 4)
                {
                    eFlag = false;
                }
            }
        }
    }
    return eFlag;
}


// Function To Check Whether The Value Entered Is A Non Negative Float 

function check_numeric(sText)
{   
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;
 
    for (i = 0; i < sText.length && IsNumber == true; i++)
    {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1)
        {
            IsNumber = false;
        }
    }
    return IsNumber;
}



/***********************************************
* Fixed ToolTip script- � Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
var tipwidth='150px' //default tooltip width
var tipbgcolor='white'  //tooltip bgcolor
var disappeardelay=250  //tooltip disappear speed onMouseout (in miliseconds)
var vertical_offset="-20px" //horizontal offset of tooltip from anchor link
var horizontal_offset="163px" //horizontal offset of tooltip from anchor link

/////No further editting needed

var ie4=document.all
var ns6=document.getElementById&&!document.all

if (ie4||ns6)
    document.write('<div id="fixedtipdiv" style="visibility:hidden;width:'+tipwidth+';background-color:'+tipbgcolor+'" ></div>')

function getposOffset(what, offsettype){
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
    var parentEl=what.offsetParent;
    while (parentEl!=null){
        totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
        parentEl=parentEl.offsetParent;
    }
    return totaloffset;
}


function showhide(obj, e, visible, hidden, tipwidth){
    if (ie4||ns6)
        dropmenuobj.style.left=dropmenuobj.style.top=-500
    if (tipwidth!=""){
        dropmenuobj.widthobj=dropmenuobj.style
        dropmenuobj.widthobj.width=tipwidth
    }
    if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
        obj.visibility=visible
    else if (e.type=="click")
        obj.visibility=hidden
}

function iecompattest(){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
    var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1
    if (whichedge=="rightedge"){
        var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
        dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
        if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
            edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
    }
    else{
        var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
        dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
        if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
            edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
    }
    return edgeoffset
}

function fixedtooltip(menucontents, obj, e, tipwidth){
    if (window.event) event.cancelBubble=true
    else if (e.stopPropagation) e.stopPropagation()
    clearhidetip()
    dropmenuobj=document.getElementById? document.getElementById("fixedtipdiv") : fixedtipdiv
    dropmenuobj.innerHTML=menucontents

    if (ie4||ns6){
        showhide(dropmenuobj.style, e, "visible", "hidden", tipwidth)
        dropmenuobj.x=getposOffset(obj, "left")
        dropmenuobj.y=getposOffset(obj, "top")
        dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
        dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
    }
}

function hidetip(e){
    if (typeof dropmenuobj!="undefined"){
        if (ie4||ns6)
            dropmenuobj.style.visibility="hidden"
    }
}

function delayhidetip(){
    if (ie4||ns6)
        delayhide=setTimeout("hidetip()",disappeardelay)
}

function clearhidetip(){
    if (typeof delayhide!="undefined")
        clearTimeout(delayhide)
}

/*-----function to compare the dates-------*/

function compareDates (value1, value2)
{
    var date1, date2;
    var month1, month2;
    var year1, year2;

    date1 = value1.substring (0, value1.indexOf ("-"));
    month1 = value1.substring (value1.indexOf ("-")+1, value1.lastIndexOf ("-"));
    year1 = value1.substring (value1.lastIndexOf ("-")+1, value1.length);

    date2 = value2.substring (0, value2.indexOf ("-"));
    month2 = value2.substring (value2.indexOf ("-")+1, value2.lastIndexOf ("-"));
    year2 = value2.substring (value2.lastIndexOf ("-")+1, value2.length);

    if (year1 > year2) return 1;
    else if (year1 < year2) return -1;
    else if (month1 > month2) return 1;
    else if (month1 < month2) return -1;
    else if (date1 > date2) return 1;
    else if (date1 < date2) return -1;
    else return 0;
}
function echeck(str) {

    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1){
        alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(at,(lat+1))!=-1){
        alert("Invalid E-mail ID")
        return false
    }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(dot,(lat+2))==-1){
        alert("Invalid E-mail ID")
        return false
    }

    if (str.indexOf(" ")!=-1){
        alert("Invalid E-mail ID")
        return false
    }

    return true
}
	
function containsAlphabets(checkString) {
    var tempString="";
    var regExp = /^[A-Za-z]$/;
    if(checkString != null && checkString != "")
    {
        for(var i = 0; i < checkString.length; i++)
        {
            if (!checkString.charAt(i).match(regExp))
            {
                return false;
            }
        }
    }
    else
    {
        return false;
    }
    return true;
}	

function isSpclChar(thetext){
var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
for (var i = 0; i < thetext.length; i++) {
    if (iChars.indexOf(thetext.charAt(i)) != -1) {
    //alert ("The box has special characters. \nThese are not allowed.\n");
     return true;
        }
    }
}


function isValidImage(thefield)
{
        var ext = thefield.split('.').pop().toLowerCase();
        if($.inArray(ext,['gif','png','jpg','jpeg'])==-1) {
                return false;
        }
    else{
            return true;
        }
}

function isValidCSV(thefield)
{
        var ext = thefield.split('.').pop().toLowerCase();
        if($.inArray(ext,['csv','xls'])==-1) {
                return false;
        }
    else{
            return true;
        }
}
