/*-- Library --*/

/*-- This is for different browsers DOM to find first child from top --*/

function firstChildMod( parentObj , childNodesCnt , elementPos )
{
    j = 0;
    for( i = 0 ; i < childNodesCnt ; i++ )
    {
        if( parentObj.childNodes[i].nodeType == 1 )
            j++;
        if( j == elementPos )
            return parentObj.childNodes[i];
    }
}

/*-- This is for different browsers DOM to find actual number of child elements --*/

function ChildNodesLength( parentObj , childNodesCnt )
{
    j = 0;
    for( i = 0 ; i < childNodesCnt ; i++ )
    {
        if( parentObj.childNodes[i].nodeType == 1 )
            j++;
    }
    return j;
}

/*-- Added for Form fields tooltips --*/
function addLoadEvent( func )
{
    var oldonload = window.onload;
    if( typeof window.onload != 'function' )
    {
        window.onload = func;
    } 
    else
    {
        window.onload = function() {
                                        oldonload();
                                        func();
                                   }
    }
}

function prepareInputsForHints()
{
    var inputs = document.getElementsByTagName("input");
	
	if (!inputs) return false;
    for( var i = 0 ; i < inputs.length ; i++ )
    {
        // test to see whether it's login or reg form or not
        // test to see if the hint span exists first
        if( inputs[i].parentNode.getElementsByTagName("span")[0] && inputs[i].parentNode.getElementsByTagName("span")[0].className == "hint" )
        {
            // the span exists!  on focus, show the hint
            inputs[i].onfocus = function () {
                                                 this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
                                            };

            // when the cursor moves away from the field, hide the hint
            inputs[i].onblur = function () {
                                                this.parentNode.getElementsByTagName("span")[0].style.display = "none";
                                           };

        }
    }
    // repeat the same tests as above for selects
    var selects = document.getElementsByTagName("select");
    for( var k = 0 ; k < selects.length ; k++ )
    {
        if ( selects[k].parentNode.getElementsByTagName("span")[0] && selects[k].parentNode.getElementsByTagName("span")[0].className == "hint" )

        {
            selects[k].onfocus = function () {
                                                this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
                                             };

            selects[k].onblur = function () {
                                                this.parentNode.getElementsByTagName("span")[0].style.display = "none";
                                            };
        }
    }
}

/*-- Place all the body onload function calls here --*/

addLoadEvent(prepareInputsForHints);

/*-- Validation functions --*/

function onlyAlphabets( inputValue )
{
    if( inputValue.match( /^[A-Za-z\ ]+$/ ) )
        return true;
    return false;
}

function onlyNumbers( inputValue )
{
    if( inputValue.match( /^[0-9]+$/ ) )
        return true;
    return false;
}

function isPhoneNumber( inputValue )
{
    if( inputValue.match( /^[0-9-]{5,20}$/ ) )
        return true;
    return false;
}

function isUserName( inputValue )
{
    if( inputValue.match( /^[a-zA-Z][\w-\.]+[a-zA-Z0-9]$/ ) )
        return true;
    return false;
}

function isMail( inputValue )
{
    //if(inputValue.match(/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/))
    if( inputValue.match( /^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,5})$/ ) )
        return true;
    return false;
}

function shadow()
{
    var bodyHeight = document.body.clientHeight;
    var bodyWidth = document.body.clientWidth;
    var shadowDiv = document.getElementById("shadow");
    shadowDiv.style.height = bodyHeight+"px";
    shadowDiv.style.width = bodyWidth+"px";
    shadowDiv.style.display = "block";
}

/*-- Ajax --*/

function createObject()
{
    var request_type;
    var browser = navigator.appName;
    if( browser == "Microsoft Internet Explorer" )
    {
        request_type = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        request_type = new XMLHttpRequest();
    }
    return request_type;

}
var http = createObject();


/*-- Top Navigation --*/

/*-- li hover for IE --*/

deltaHover = function() {
	if (!document.getElementById("delta-nav")) return false;
	var deltaEls = document.getElementById("delta-nav").getElementsByTagName("LI");
	for (var i=0; i<deltaEls.length; i++) {
		deltaEls[i].onmouseover=function() {
			this.className+=" deltahover";
		}
		deltaEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" deltahover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", deltaHover);


/*-- Menu timeout --*/

var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{  
	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('.delta-nav-menu').css('left', 'auto');
	menu = $(this).css('background-position', '0 -38px');
}

function jsddm_close()
{  
	if(ddmenuitem) 
	{
		ddmenuitem.css('left', '-999em');
		menu.css('background-position', '0 0px');
	}
}

function jsddm_timer()
{  
	closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{  
	if(closetimer)
	{ 
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

$(document).ready(function()
{
	if ( $.browser.msie && $.browser.version < 7 ){}
	else
	{
		$('#delta-nav > li').bind('mouseover', jsddm_open)
		$('#delta-nav > li').bind('mouseout',  jsddm_timer)
	} 
});

document.onclick = jsddm_close;


/*-- Contact Us --*/


/*-- Contact Us select options JS --*/

function changeProductsStatus()
{
	var CDF_obj = document.getElementById("whydelta");
	if (!CDF_obj) return false;
	CDF_obj.onchange = function () {
										if (this.value == "Delta Products")
										{
											document.getElementById("product").disabled = false;
										}
										else
										{		
											document.getElementById("product").selectedIndex = 0;
											document.getElementById("product").disabled = true;
										}
									};
}	

addLoadEvent(changeProductsStatus);

/*-- Contact Delta Validation --*/

function contactDeltaValidate()
{
    var errs = 0;
    var errmsgs = "";

    var email = document.contact_delta.email.value;
    var emailerr = 0;
    if( email == "" )
    {
        errmsgs += "Please enter your E-mail.<br/>";
        errs++;
        emailerr++;
    }
    else
    {
        if( !isMail(email) )
        {
            errmsgs+="Please enter valid E-mail. &nbsp;&nbsp;&nbsp;&nbsp;<strong>NOTE:</strong> Only _, -, ., @ special characters are allowed.<br/>";
            errs++;
            emailerr++;
        }
    }

    var confirm_email = document.contact_delta.confirm_email.value;
    if( confirm_email == "" && !emailerr )
    {
        errmsgs += "Please re-enter your E-mail.<br/>";
        errs++;
    }
    else
    {
        if( confirm_email != email && !emailerr )
        {
            errmsgs += "The two email addresses do not match. &nbsp;&nbsp;&nbsp;&nbsp;<strong>NOTE:</strong> Please enter same E-mail ID.<br/>";
            errs++;
        }
    }

    var fname = document.contact_delta.fname.value;
    if( fname == "" )
    {
        errmsgs += "Please enter your first name.<br/>";
        errs++;
    }
    else
    {
        if( !onlyAlphabets(fname) || fname.length < 3 )
        {
            errmsgs+="Please enter valid first name. &nbsp;&nbsp;&nbsp;&nbsp;<strong>NOTE:</strong> Only alphabets & spaces are allowed. Must be at least 3 characters in length.<br/>";
            errs++;
        }
    }

    var lname = document.contact_delta.lname.value;
    if( lname == "" )
    {
        errmsgs +="Please enter your last name.<br/>";
        errs ++;
    }
    else
    {
        if( !onlyAlphabets(lname) )
        {
            errmsgs += "Please enter valid last name. &nbsp;&nbsp;&nbsp;&nbsp;<strong>NOTE:</strong> Only alphabets & spaces are allowed.<br/>";
            errs++;
        }
    }

    var cnumber = document.contact_delta.cnumber.value;
    if( cnumber != "" && !isPhoneNumber(cnumber) )

    {
        errmsgs += "Please enter valid contact number. &nbsp;&nbsp;&nbsp;&nbsp;<strong>NOTE:</strong> Only numbers & '-' are allowed.<br/>";

        errs++;
    }

    var mnumber = document.contact_delta.mnumber.value;
    if( mnumber != "" && !isPhoneNumber(mnumber) )

    {
        errmsgs += "Please enter valid mobile number. &nbsp;&nbsp;&nbsp;&nbsp;<strong>NOTE:</strong> Only numbers & '-' are allowed.<br/>";
        errs++;
    }


    var city = document.contact_delta.city.value;
    if( city != "" && !onlyAlphabets(city) )
    {
        errmsgs += "Please enter valid city name. &nbsp;&nbsp;&nbsp;&nbsp;<strong>NOTE:</strong> Only alphabets & spaces are allowed.<br/>";
        errs++;

    }

    var state = document.contact_delta.state.value;
    if( state != "" && !onlyAlphabets(state) )

    {
        errmsgs += "Please enter valid state name. &nbsp;&nbsp;&nbsp;&nbsp;<strong>NOTE:</strong> Only alphabets & spaces are allowed.<br/>";

        errs++;
    }
	
    var country = document.contact_delta.country.value;
    if( country != "" && !onlyAlphabets(country) )
    {
        errmsgs += "Please enter valid country name. &nbsp;&nbsp;&nbsp;&nbsp;<strong>NOTE:</strong> Only alphabets & spaces are allowed.<br/>";
        errs++;
    }	

    var pin = document.contact_delta.pin.value;
    if( pin != "" && !onlyNumbers(pin) )

    {
        errmsgs += "Please enter valid PIN/ZIP code. &nbsp;&nbsp;&nbsp;&nbsp;<strong>NOTE:</strong> Only numbers are allowed.<br/>";

        errs++;
    }

    var errMsgDiv = document.getElementById("errmsg_container");
    if( errs )
    {
        errMsgDiv.innerHTML = "<div id='err_title'>Please Correct the following errors</div><div id='errmsg'>" + errmsgs + "</div>";
        errMsgDiv.style.display = "block";
        return false;
    }
    errMsgDiv.style.display = "none";

    shadow();
    contactDelta();
    return false;
}

/*-- Contact Delta form Ajax processing --*/

/*== Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue --*/

var nocache = 0;

function contactDelta()
{


    var email = escape(document.getElementById('email').value);
    var fname = escape(document.getElementById('fname').value);
    var lname = escape(document.getElementById('lname').value);
    var cnumber = escape(document.getElementById('cnumber').value);
    var mnumber = escape(document.getElementById('mnumber').value);
    var company = escape(document.getElementById('company').value);
    var address = escape(document.getElementById('address').value);
    var city = escape(document.getElementById('city').value);
    var state = escape(document.getElementById('state').value);
    var country = escape(document.getElementById('country').value);	
    var pin = escape(document.getElementById('pin').value);
	var whydelta = escape(document.getElementById('whydelta').value);
    var product = escape(document.getElementById('product').value);
    var comment = escape(document.getElementById('comment').value);	

    http.open( "POST" , 'process-enquiry.php' , true );
    http.onreadystatechange = contactDeltaStatus;
    http.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" );
    http.send( "&email=" + email + "&fname=" + fname + "&lname=" + lname + "&cnumber=" + cnumber + "&mnumber=" + mnumber + "&company=" + company + "&address=" + address + "&city=" + city + "&state=" + state + "&country=" + country + "&pin=" + pin + "&product=" + product + "&whydelta=" + whydelta + "&comment=" + comment );
}


function contactDeltaStatus()
{
    if( http.readyState == 4 )
    {
        var response = http.responseText;
        var shadowDiv = document.getElementById("shadow");
        shadowDiv.style.display = "none";

        if( response == 0 )
        {
            var errMsgDiv = document.getElementById("errmsg_container");
            errMsgDiv.innerHTML = "<div id='err_title' class='err_title_mod'>The server has encountered a problem. We regret the inconvenience. Please check back soon.</div>";
            errMsgDiv.style.display = "block";
            return;
        }

        if(response == 1)
        {
            // Locate form on page
            var regFormDiv = document.getElementById("contact_delta_form_content");
            regFormDiv.innerHTML = "<div class='alert'><p><b>Thank you</b>.</p><p>Our representative will get back to you shortly.</p></div>";
            return;
        }

    }
}


/*-- News scrolling --*/

$('#scrollup_news').cycle({ 
    fx:      'scrollUp', 
    speed:    1000,
	timeout:  5000,
    pause:   1,
	pagerAnchorBuilder: function(idx, slide) {
	return '';
	}
});
