// add onLoad events
function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

// add onUnload events
function addUnloadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
		window.onunload = func;
	} else {
		window.onunload = function() {
			oldonunload();
			func();
		}
	}
}

var closeyourself = false;
var exitPopupNeeded = false;

//dummy
function dummy() {;}

// Renamed above function
function privacy() {;}

// exitpopup for ie only
function leave(popPage,width,height,scrollbar) {
  if ( !exitPopupNeeded || (window.name=='nopopup') ) return true;
    var pwidth = 750;
    var pheight = 520;
  if ( width>0 ) { 
    pwidth = width;
    pheight = height;
  }
  var args = 'status:no; help:no; dialogHeight:'+pheight+'px; dialogWidth:'+pwidth+'px;'
  if(document.all) 
    showModalDialog(popPage,'',args);
}

// this checks the format of the email address
function em_chk(str){
  if(!str.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)) {
    return false;
  } else {
    return true;
  }
}


function checkradios (elem,n) {
	var c = 0;
	for ( i=0; i<n; i++ ) {
		//alert (elem[i] +": "+ elem[i].checked);
		if ( elem[i].checked ) c++;
	}
	if (c==0) return false;
	else return true;
}
function  checkordersurveyform () {
	frm = document.forms['ordersurveyform'];
	if (!frm.mainreason.value) {
    alert ("Please answer Question 1"); frm.mainreason.focus(); return false;
  } else if ( !checkradios(frm.buyfor,2) ) {
    alert ("Please answer Question 2");
    frm.buyfor[0].focus(); 
    return false;
  } else if ( frm.buyfor[1].checked && !frm.buyfortext.value) {
    alert ("Please specify WHO you bought it for *in the box provided* - related to Question 2");
    frm.buyfortext.focus(); 
		return false;
  } else if ( !checkradios(frm.gender,2) ) {
    alert ("Please answer Question 3");
    frm.gender[0].focus(); 
    return false;
  } else if ( !checkradios(frm.agegroup,6) ) {
    alert ("Please answer Question 4");
    frm.agegroup[0].focus(); 
    return false;
  } else if ( !checkradios(frm.usingmob,2) ) {
    alert ("Please answer Question 5");
    frm.usingmob[0].focus(); 
    return false;
  } else if ( frm.usingmob[0].checked && !checkradios(frm.usinglong,6) ) {
    alert ("Please select *how long* you've been using a mobile - In Question 5");
    frm.usinglong[0].focus(); 
    return false;
  } else if ( !checkradios(frm.carrier,10) ) {
    alert ("Please answer Question 6");
    frm.carrier[0].focus(); 
    return false;
  } else if ( frm.carrier[9].checked && !frm.carrierother.value)  {
    alert ("Please specify *in the box provided* - in Question 6");
    frm.carrierother.focus();
    return false;
  } else if ( !checkradios(frm.plan,6) ) {
    alert ("Please answer Question 7");
    frm.plan[0].focus(); 
    return false;
  } else if ( frm.plan[5].checked && !frm.planother.value)  {
    alert ("Please specify *in the box provided* - in Question 7");
    frm.planother.focus();
    return false;
  } else 	if (!frm.providerreason.value) {
    alert ("Please answer Question 8"); frm.providerreason.focus(); return false;
  } else if ( !checkradios(frm.spending,9) ) {
    alert ("Please answer Question 9");
    frm.spending[0].focus(); 
    return false;
  } else if ( !checkradios(frm.usefor,5) ) {
    alert ("Please answer Question 10");
    frm.usefor[0].focus(); 
    return false;
  } else if ( !checkradios(frm.teenager,2) ) {
    alert ("Please answer Question 11");
    frm.teenager[0].focus(); 
    return false;
  } else if ( frm.teenager[0].checked && !checkradios(frm.teenagerspend,8) ) {
    alert ("Please specify *How Much You Spend* - in Question 11");
    frm.teenagerspend[0].focus(); 
    return false;
  } else if ( !checkradios(frm.control,2) ) {
    alert ("Please answer Question 12");
    frm.control[0].focus(); 
    return false;
  }
  return true;
}

// this checks the newsletter form and alerts if smthing was not filled correctly
function check_newsletter_form(id,moveon,closeyourself){
  str = 'newsletterform' + id;
  frm=document.forms[str];
  if (!frm.firstname.value) {
    alert ("Please include your first name");
    frm.firstname.focus();
    return false;
  } 
  /*
  else if (!frm.lastname.value) {
    alert ("Please include your last name");
    frm.lastname.focus();
    return false;
  } 
  */
  else if (!frm.email.value) {
    alert ("Please include your email address");
    frm.email.focus();
    return false;
  } else if ( !em_chk(frm.email.value) ) {
    alert ("Please check your email address");
    frm.email.focus();
    return false;
  }
  
  exitPopupNeeded=false;
  if (closeyourself==1) { 
  	self.close();
  }
  if (moveon) { document.location.href='main.html'; }  
  return true;
}

// to open the privacy window
function oppwin ( loc, pWidth, pHeight ) {
  if (!pWidth) var pWidth = 500;
  if (!pHeight) var pHeight = 400;
  var xPos = (screen.width/2)-(pWidth/2);
  var yPos = (screen.height/2)-(pHeight/2);
  var ppWin = "";
  //Opera most likely in tabbed mode, so calculate from the win size instead of screen size
  if (navigator.appName=="Opera") 
  yPos = (window.innerHeight/2)-(pHeight/2);
  
  ppWin = window.open ("","ppWin","width=" + pWidth + ",height=" + pHeight + ",status=0,menubar=0,location=0,resizable=1,titlebar=0,scrollbars=1,left=" + xPos + ",top=" + yPos );
  ppWin.location.href = loc;
} 
