/**
 * FILE: global_functions.js
 * DESCRIPTION: This file contains global javascript functions for SFDC
 *              Web site.
 * CREATED: 02/03/2001
**/

/**
 * Opens a new window
**/

var curPopupWindow = null;

function openWindow(url, winName, width, height, center) {
	
   var xposition = 50; // Postions the window vertically in px
   var yposition = 50; // Postions the window horizontally in px


   if ((parseInt(navigator.appVersion) >= 4 ) && (center)){
       xposition = (screen.width - 800) / 2;
       yposition = (screen.height - 600) / 2;
   } 

   // Features to specify for a new window
   args = "width=" + width + ","
   + "height=" + height + ","
   + "location=0,"
   + "menubar=0,"
   + "resizable=1,"
   + "scrollbars=1,"
   + "status=0,"
   + "titlebar=0,"
   + "toolbar=1,"
   + "hotkeys=0,"
   + "screenx=" + xposition + ","  //NN Only
   + "screeny=" + yposition + ","  //NN Only
   + "left=" + xposition + ","     //IE Only
   + "top=" + yposition;           //IE Only

   // Performs the opening of the window.
   	if (curPopupWindow != null) {
	   
		if (!curPopupWindow.closed) {
			curPopupWindow.close();
		}
		curPopupWindow = null;
	}
	curPopupWindow = window.open(url, winName, args, false);
	curPopupWindow.focus();

}

/**
 * Verify that an email addres is valid
 * Written by Paolo Wales (paolo@taize.fr)
**/

function isValidEmail(emailad) {

   var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
   var check=/@[\w\-]+\./;
   var checkend=/\.[a-zA-Z]{2,3}$/;

   if(((emailad.search(exclude) != -1) ||
       (emailad.search(check)) == -1) ||
       (emailad.search(checkend) == -1)){
      return false;
   } else {
      return true;
   }
}



/**
 * Validate phone number for signup
**/

function isValidPhoneNumber(num) {
    var digits = 0;
    if (num == null) return false;
    for( i=0; i<num.length; i++ ){
    	var c = num.charCodeAt(i);  
    	//convert the i-th character to ascii code value
    	if( (c>=48) && (c<=57) ) digits++;
    }	
    return (digits >= 10);
}

/**
 * Validation for numeric vales in ROI Calculator
 * Sends error for negative, zero vallue, Null, and non-numeric characters
**/

function isValidNumber(num) {
    var digits = 0;
    for( i=0; i<num.length; i++ ){
    var c = num.charCodeAt(i); 
        if( (c<=47) || (c>=58) ) digits++;
    }
    if (num.length == 0) {
        return false;
    }
        if (num.length == 1 && num == 0) {
        return false;    
    }
    if (digits > 0) {
        return false;
    }
    else {
        return true;
    }
}


/**
 * Image swaping (mouseover) for the Web site
**/

function swapImage(daImage, daSrc){
var objStr, obj;
	if(document.images){
		if (typeof(daImage) == 'string') {
			objStr = 'document.' + daImage;
			obj = eval(objStr);
			obj.src = daSrc;
		} else if ((typeof(daImage) == 'object') && daImage && daImage.src) {
			daImage.src = daSrc;
		}
	}
}

/** 
 * Validate document for signup
**/

function validateLocation() {
	if (document.forms[0].country.value == 0)
		alert("You must select a location");
}

/** 
 * Get cookie for login
**/

function getCookieValue(cookieName) {
  var c = document.cookie;
  var idx = c.indexOf('login=');
  if ( idx == -1) return "";
  idx += 'login='.length;
  var end = c.indexOf(';',idx);
  if ( end == -1) end = c.length;
  return c.substring(idx,end);
}

/** 
 * Load cookie (user name) for login
**/

function loader() {
	var username = getCookieValue("login");
	if (username.length > 0) {
		document.login_nop.username.value = username;
	    document.login.pw.focus();
	} else {
		document.login_nop.username.focus();
	}
}

function handleLogin() {
	document.login.un.value = document.login_nop.username.value;
	var nextyear = new Date();
	nextyear.setFullYear(nextyear.getFullYear()+1);
	document.cookie = "login=" + document.login.un.value + 
					  "; expires=" + nextyear.toGMTString();
}

/**
 * Preload Images 
**/

function preloadImages() {
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

/**
 * Close tour window and bring up signup screen(s)
**/

function switchWindows(url) {
   top.opener.location.href=url;	
   top.close();
}

function switchWindowsNoClose(url) {
   top.opener.location.href = url;	
}

/**
 * Validate partner forms.
**/

function partnerValidation() {
   if (document.partnerForm.first_name.value == 0) {
      alert("You must enter a First Name");
      return false;
   } else if (document.partnerForm.last_name.value == 0) {
      alert("You must enter a Last Name");
      return false;
   } else if (document.partnerForm.phone.value == 0) {
      alert("You must enter a Phone Number");
      return false;
   } else if (document.partnerForm.email.value == 0) {
      alert("You must enter an Email Address");
      return false;
   } else if (document.partnerForm.company.value == 0) {
      alert("You must enter a Company Name");
      return false;
   } else if (document.partnerForm.street.value == 0) {
      alert("You must enter a Street Address");
      return false;
   } else if (document.partnerForm.city.value == 0) {
      alert("You must enter a City");
      return false;
   } else if (document.partnerForm.state.selectedIndex == "0") {
      alert("You must enter a State");
      return false;
   } else if (document.partnerForm.zip.value == 0) {
      alert("You must enter a Zip/Postal Code");
      return false;
   } else if (document.partnerForm.country.selectedIndex == "0") {
      alert("You must enter a Country Code");
      return false;
   } else {
     return true;
   }
}

/**
 * Validate Contact Me form.
**/

function contactValidation() {
   if (document.contactForm.first_name.value == 0) {
      alert("You must enter a First Name");
      return false;
   } else if (document.contactForm.last_name.value == 0) {
      alert("You must enter a Last Name");
      return false;
   } else if (document.contactForm.phone.value == 0) {
      alert("You must enter a Phone Number");
      return false;
   } else if (document.contactForm.email.value == 0) {
      alert("You must enter an Email Address");
      return false;
   } else if (document.contactForm.company.value == 0) {
      alert("You must enter a Company Name");
      return false;
   } else if (document.contactForm.street.value == 0) {
      alert("You must enter a Street Address");
      return false;
   } else if (document.contactForm.city.value == 0) {
      alert("You must enter a City");
      return false;
   } else if (document.contactForm.state.selectedIndex == "0") {
      alert("You must enter a State");
      return false;
   } else if (document.contactForm.zip.value == 0) {
      alert("You must enter a Zip/Postal Code");
      return false;
   } else if (document.contactForm.country.selectedIndex == "0") {
      alert("You must enter a Country Code");
      return false;
   } else if (document.contactForm.industry.selectedIndex == "0") {
      alert("You must enter an Inudustry");
      return false;  
   } else if (document.contactForm.revenue.selectedIndex == "0") {
      alert("You must enter a Company Revenue");
      return false; 
   } else {
     return true;
   }
}

/**
 * Validate billing inquiry feilds
**/

function billingValidation() {
   if (document.webtocase.first_name.value == 0) {
      alert("You must enter a Name");
      return false;
   } else if (document.webtocase.company.value == 0) {
      alert("You must enter a Company Name");
      return false;
   } else if (document.webtocase.email.value == 0) {
      alert("You must enter an Email Address");
      return false;
   } else if (document.webtocase.subject.value == 0) {
      alert("You must enter a Subject");
      return false;
   } else if (document.webtocase.description.value == 0) {
      alert("You must enter a Question");
      return false;
   } else {
     return true;
   }
}

/* 
 * Used to get query string arguments before user
 * has a chance to alter them on the billing inqury form.
 * (billing_inquiry.jsp)
 */

function winInit() {
	var args = getArgs();

	//show email field if not supplied -
	//set value to empty string if null
	var eml = args["eml"];
	document.webtocase.email.value = (eml != null ? eml : "");

	//show name field if not supplied -
	//set value to empty string if null
	var nm = args["nm"];
	document.webtocase.first_name.value = (nm != null ? nm : "");

	//show company field if not supplied -
	//set value to empty string if null
	var co = args["co"];
	document.webtocase.company.value  = (co != null ? co : "");
}

/* parses name=value argument pairs from
 * the query string of the URL. It stores the pairs in 
 * properties of an object and returns that object from the
 * billing inqury form. (billing_inquiry.jsp)
 */

function getArgs() {
    var args = new Object();

    // Get query string.
    var query = location.search.substring(1);  

    // Break at ampersand.
    var pairs = query.split("&"); 

       for (var i = 0; i < pairs.length; i++) {
       // Look for "name=value"
       var pos = pairs[i].indexOf('=');

       // If not found, skip.
       if (pos == -1) continue; 
	   		
       // Extract the name.              
       var argname = pairs[i].substring(0,pos);  
			
       // Extract the value.
       var value = pairs[i].substring(pos+1);
			
       // Store as a property. 
       args[argname] = unescape(value);       
     }
   return args;
}