/*=====================================================================
 Filename: pp_utils.js -- Written by ndelmedico@waca.com
 Created: 7/14/2003 -- Last Updated: 10/27/2005
 This file contains client-side scripts used for Protection Plus
======================================================================*/

/*=====================================================================
Description: Attaches events to nav elements in DOM supporting browsers
======================================================================*/
/*window.onload = function() {
    if (document.getElementById) {
        setupEventCapture("aboutlink"); setupEventCapture("custlink"); setupEventCapture("partlink"); setupEventCapture("helplink");
        if (document.getElementById && window.attachEvent && !screen.pixelDepth) {window.attachEvent("onresize", setupMinWidth); setupMinWidth();}
    }
}*/

/*=====================================================================
Description: Adds function to onload event while preserving old onload
======================================================================*/
function ppAddLoadEvent(thisFunction) {
    var curOnload = window.onload;
    if (typeof window.onload != "function") {
      window.onload = thisFunction;
    } else {
      window.onload = function() {curOnload(); thisFunction();}
    }
}

/*=====================================================================
Description: Used to fix IE's lack of support for min-width
======================================================================*/
function setupMinWidth() {
    if (document.body.clientWidth < 767) {document.getElementById("header").style.width = "767px"; document.getElementById("footer").style.width = "767px";}
    else if (document.body.clientWidth > 767) {document.getElementById("header").style.width = "auto"; document.getElementById("footer").style.width = "auto";}
    if (document.getElementById("content")) {document.getElementById("content").style.height = "275px";}
    if (document.getElementById("fullcol")) {document.getElementById("fullcol").style.height = "275px";}
}

/*=====================================================================
Description: Attaches over and out events to navigation elements
Parameters: Navigation link ID to setup
======================================================================*/
function setupEventCapture(sID) {
    if (document.getElementById(sID).addEventListener) {
        document.getElementById(sID).addEventListener("mouseover", navOver, false);
        document.getElementById(sID).addEventListener("mouseout", navOut, false);
    } else if (document.getElementById(sID).attachEvent) {
        document.getElementById(sID).attachEvent("onmouseover", navOver);
        document.getElementById(sID).attachEvent("onmouseout", navOut);
    }
}

/*=====================================================================
Description: Handles mouseover actions for navigation links
Parameters: None - captures incoming event target
======================================================================*/
/*function navOver(eEvent) {
    var oElem; var sMsg; var sMenu; var iLeft;
    if (eEvent.target) {oElem = eEvent.target;} else {oElem = eEvent.srcElement;}
    switch(oElem.id) {
        case "aboutlink": sMsg = "About Protection Plus"; sMenu = "About"; iLeft = 246; break;
        case "custlink": sMsg = "Protection Plus Customer Section"; sMenu = "Customers"; iLeft = 355; break;
        case "partlink": sMsg = "Protection Plus Partner Section"; sMenu = "Partners"; iLeft = 476; break;
        case "helplink": sMsg = "Need Help?"; sMenu = "Help"; iLeft = 585; break;
    }
    document.getElementById(oElem.id).className = "navon";
    navOff(oElem.id); cFrame.ShowMenu(sMenu, iLeft, 90, false);
    window.status = sMsg; return true;
}*/

function ppValidatePasswordControls(strStep) {
    var oForm = document.frmForgot; var sErrors = "";
	if (strStep == 1)
	{
		if (oForm.txtSrcKey != null) {
			if (Trim(oForm.txtSrcKey.value).length < 1) {sErrors += "\n-  Please enter your user name.";}
    		if (!isValidChar(Trim(oForm.txtSrcKey.value), sAlpha + sNumeric)) {sErrors += "\n-  User name contains invalid characters.";}
		}
		//if (oForm.txtemail != null) {if (!isEmail(oForm.txtemail.value)) {sErrors += "\n-  " + sEmail;}}
	}
	else if (strStep == 2)
	{
		if (oForm.txtAnswer != null) {if (Trim(oForm.txtAnswer.value).length < 1) {sErrors += "\n-  Please enter your answer.";}}
	}
	else if (strStep == 3) //**********CHANGE BACK TO 3 WHEN DONE******************
	{	
		if (oForm.txtPassword != null) {
			if (Trim(oForm.txtPassword.value) == "") {sErrors += "\n-  Please enter a password.";}
			if (Trim(oForm.txtPassCopy.value) == "") {sErrors += "\n-  Please verify your password.";}
			if (Trim(oForm.txtPassCopy.value) != Trim(oForm.txtPassword.value)) {sErrors += "\n-  Password entries don\'t match.";}
			if (Trim(oForm.txtPassword.value).length < 5 ) {sErrors += "\n-  Password must be at least 5 characters long.";}
		}
		if (oForm.selQuestion != null) {
			if (Trim(oForm.selQuestion.value) == "") {sErrors += "\n-  Please select your security question.";}
			if (Trim(oForm.txtAnswer.value) == "") {sErrors += "\n-  Please enter your security answer.";}
		}		
		if (!isEmail(oForm.txtEmail.value)) {sErrors += "\n-  Please enter a valid email address.";}		
	}
	
    if (sErrors != "") {displayError(sErrors); return false;}
    else {
		oForm.hidCommand.value = "submit";		
		return true;
	}
}


/*=====================================================================
Description: Handles mouseout actions for navigation links
Parameters: None - captures incoming event target
======================================================================*/
function navOut(eEvent) {
    var oElem;
    if (eEvent.target) {oElem = eEvent.target;} else {oElem = eEvent.srcElement;}
    if (getGroup() != getDivID(oElem.id)) {document.getElementById(oElem.id).className = "navoff";}
    navCleaner(); cFrame.tHideAll();
    return true;
}

/*=====================================================================
Description: Resets mouseover images to off state
Parameters: An ID of a nav link to ignore
======================================================================*/
function navOff(sID) {
    if (sID != "aboutlink") {document.getElementById("aboutlink").className = "navoff";}
    if (sID != "custlink") {document.getElementById("custlink").className = "navoff";}
    if (sID != "partlink") {document.getElementById("partlink").className = "navoff";}
    if (sID != "helplink") {document.getElementById("helplink").className = "navoff";}
}

/*=====================================================================
Description: Resets navigation links to off state
======================================================================*/
function navCleaner() {
    if (getGroup() == null) {navOff("");}
    else {window.setTimeout("navCleaner();", 1500);}
}

/*=====================================================================
Description: Returns the div ID corresponding to a navigation link ID 
Parameters: A navigation link ID
Returns: String containing the ID of the corresponding menu div
======================================================================*/
function getDivID(sNavID) {
    var sID = null;
    switch(sNavID) {
        case "aboutlink": sID = "About"; break;
        case "custlink": sID = "Customers"; break;
        case "partlink": sID = "Partners"; break;
        case "helplink": sID = "Help"; break;
    }
    return sID;
}

/*=====================================================================
Description: Returns the ID of the open menu div -- null if none open
======================================================================*/
/*function getGroup() {
    var iOM = nOM; var iIdx = 0;
    for(nOM = 1; nOM <= iOM; nOM++) {if(f31()) {iIdx = nOM; break;}}
    nOM = iOM;
    if (iIdx) {return OpenMenus[iIdx].obj.id;} else {return null;}
}*/

/*=====================================================================
Description: Opens forgot password window or register for protection plus account.
======================================================================*/

function ppEnterQuestion() {
	var x = confirm('                       ****We have added an additional security measure.****\n\n  You need to add a security question and answer to your profile, otherwise you will not be able to procede.\n  This will add more security to your account.  It will take less than a minute.\n  Click OK to continue.  If you click CANCEL, you will not be able to access your account info.  Thank you.');
	if (x==true){		
		z = window.open("/protectionplus/pp/shop/pp_forgotpass.asp?step=3", "winEnterSecurityQuestion", "status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=400,height=240");	
		z.focus();
		window.location="/protectionplus/index.asp";
	}
	else{
		window.location="/protectionplus/index.asp";
	}
}

function ppForgot(sWhat) {	
	switch(sWhat) {		
    	case "Password": window.open("/protectionplus/pp/shop/pp_forgotpass.asp", "winForgotPass", "status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=400,height=340");	break;		
		
		case "Username": {			
			if (self.name == "winForgotPass"){				
				opener.location="/protectionplus/pp/partners/pp_apply.asp";				
				self.close();				
			}
			else {window.location="/protectionplus/pp/partners/pp_apply.asp";}			
			break;
		}
	}
}

function ppEquipTypes(ItemCode) {
    this.open("/protectionplus/pp/customers/manage/pp_pop_equiptypes.asp?ItemCode=" + ItemCode, "winEquipTypes", "status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,width=350,height=300");
}

/*=====================================================================
Description: Opens change password window
======================================================================*/
function ppProfile() {
    this.open("/protectionplus/pp/shop/pp_profile.asp", "winProfile", "status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=320,height=200");
}

/*=====================================================================
Description: Validates and submits Florida appointment form.
======================================================================*/
function ppFloridaAppointment() {
    var sErrors = ""; var oForm = document.frmAppoint;
    if (Trim(oForm.txtLicense.value) == "") {sErrors += "\n-  Please enter a 2-52 License #.";}
    if (!isDate(oForm.txtIssueDate.value, '')) {sErrors += "\n-  Issue date must be in mm/dd/yyyy format.";}
    if (Trim(oForm.txtName.value).length < 3) {sErrors += "\n-  Please enter the name on the license.";}
    if (Trim(oForm.txtAddress.value) == "") {sErrors += "\n-  Please enter an Address.";}
    if (!isValidChar(Trim(oForm.txtAddress.value), sAlpha + sNumeric + "#&.")) {sErrors += "\n-  Your Address contains invalid characters.";}
    if (Trim(oForm.txtCity.value) == "") {sErrors += "\n-  Please enter a City.";}
    if (!isValidChar(Trim(oForm.txtCity.value), sAlpha + sNumeric + "'.")) {sErrors += "\n-  Your City contains invalid characters.";}
    if (oForm.selState.options[oForm.selState.options.selectedIndex].value == "") {sErrors += "\n-  Please select a State.";}
    if (!isValidZip(oForm.txtZip.value)) {sErrors += "\n-  " + sPostal;}
    if (Trim(oForm.txtOwner.value) == "") {sErrors += "\n-  Please enter owners name.";}
    if (oForm.txtPhArea.value.length < 3 || oForm.txtPhPre.value.length < 3 || oForm.txtPhNum.value.length < 4) {sErrors += "\n-  Please complete your Phone Number.";}
    if (!isNumeric(oForm.txtPhArea.value) || !isNumeric(oForm.txtPhPre.value) || !isNumeric(oForm.txtPhNum.value)) {sErrors += "\n-  Phone Number must be numeric.";}
    if (!isNumeric(oForm.txtFaxArea.value) || !isNumeric(oForm.txtFaxPre.value) || !isNumeric(oForm.txtFaxNum.value)) {sErrors += "\n-  Fax Number must be numeric.";}
    if (!isEmail(oForm.txtEmail.value)) {sErrors += "\n-  " + sEmail;}
    if (sErrors != "") {displayError(sErrors);}
	// updated 11/11/2007. GV
    else {oForm.hidCommand.value = "submit";oForm.submit();}
}

/*=====================================================================
Description: Opens popup window
Parameters: URL, Window Name, Window Width, Window Height, Scrollbars
======================================================================*/
function ppPopWin(sURL, sName, iWidth, iHeight, sScroll) {
    var iLeft = (screen.width - iWidth) / 2;
    var iTop = (screen.height - iHeight) / 2;
    var sProps = "height=" + iHeight + ",width=" + iWidth + ",top= " + iTop + ",left=" + iLeft + ",scrollbars=" + sScroll + ",resizable=no";
    var oWin = window.open(sURL, sName, sProps);
    oWin.focus();
}

function ppChangePassword()
{
	var sErrors = ""; 
	var oForm = document.forms['frmProfile'];
	if (Trim(oForm.txtLoginId.value) == "") {sErrors += "\n-  Please enter a login ID.";}
	if (Trim(oForm.txtPassword.value) == "") {sErrors += "\n-  Please enter a password.";}
	if (Trim(oForm.txtPassCopy.value) == "") {sErrors += "\n-  Please verify your password.";}
	if (Trim(oForm.txtPassCopy.value) != Trim(oForm.txtPassword.value)) {sErrors += "\n-  Password entries don\'t match.";}
	if (Trim(oForm.txtEmail.value) == "") {sErrors += "\n-  Please enter your email.";}
	if (Trim(oForm.txtEmail.value) != "") {if (!isEmail(oForm.txtEmail.value)) {sErrors += "\n-  " + sEmail;}}
	if (sErrors != "") 
	{
		displayError(sErrors);		
	}
	else
	{
		oForm.submit();
	}
}


