// pl.js - JavaScript Document

function xmlhttpPost(strURL,action,postStr) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			updatepage(self.xmlHttpReq.responseText,action);
		}
	}
	self.xmlHttpReq.send(postStr);
}

function xmlhttpPost2(strURL,action,postStr) {
	var xmlHttpReq2 = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq2 = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq2 = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq2.open('POST', strURL, true);
	self.xmlHttpReq2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq2.onreadystatechange = function() {
		if (self.xmlHttpReq2.readyState == 4) {
			updatepage(self.xmlHttpReq2.responseText,action);
		}
	}
	self.xmlHttpReq2.send(postStr);
}

function xmlhttpGet(strURL,action,getStr) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('GET', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			updatepage(self.xmlHttpReq.responseText,action);
		}
	}
	self.xmlHttpReq.send(getStr);
}

function req1(strURL,action) {
	var xmlHttpReq1 = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq1 = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq1 = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq1.open('POST', strURL, true);
	self.xmlHttpReq1.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq1.onreadystatechange = function() {
		if (self.xmlHttpReq1.readyState == 4) {
			updatepage(self.xmlHttpReq1.responseText,action);
		}
	}
	self.xmlHttpReq1.send();
}

function req2(strURL,action) {
	var xmlHttpReq2 = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq2 = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq2 = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq2.open('POST', strURL, true);
	self.xmlHttpReq2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq2.onreadystatechange = function() {
		if (self.xmlHttpReq2.readyState == 4) {
			updatepage(self.xmlHttpReq2.responseText,action);
		}
	}
	self.xmlHttpReq2.send();
}

function getreq1(strURL,action,getStr) {
	var xmlHttpReqg1 = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReqg1 = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReqg1 = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReqg1.open('GET', strURL, true);
	self.xmlHttpReqg1.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReqg1.onreadystatechange = function() {
		if (self.xmlHttpReqg1.readyState == 4) {
			updatepage(self.xmlHttpReqg1.responseText,action);
		}
	}
	self.xmlHttpReqg1.send(getStr);
}

function updatepage(str,action) {
// alert(str); alert(action);

var refreshUI = "YES";

	if (action == "INCRPICVIEW") {
		refreshUI = "NO";
	} else
    {
		var divdata = document.getElementById("divdata");
	}
	
 		//loadingImage("ajaxloadimage",false);   //============================================================================
    if (refreshUI == "YES") { // REFRESH THE REGION ONLY IF REFRESHUI SET TO "YES"
		divdata.innerHTML = str;
		divdata.style.display = "";
	}
	else return flase;

    //============================================================================
	
}

function required(colvalue,colname) {
	if (colvalue == '' || colvalue == null) {
		alert("Please enter " + colname + ".");
		return false;
	}
	return true;
}

function validateEmailv2(email)
{
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    if(email.length <= 0)
	{
	  return true;
	}
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}

function cancelTask(divId) {
	document.getElementById(divId).style.display = "none";
}

function showBasicWorksheet(operation) {
	var pStr = "op=BASICOPSFORM&operation=" + operation;
	xmlhttpPost("shwqstgenerate.php","BASICOPSFORM",pStr);
}

function generateWorkSheet(operation) {

    var xNegFlag = document.getElementById("includenegative1").value;
    var yNegFlag = document.getElementById("includenegative2").value;	
    var xvalue = 0;
	if (xNegFlag == "N") {
		for (var i=0; i<6; i++) {
		   if (document.xvalueform.xvalue[i].checked) {
				xvalue = document.xvalueform.xvalue[i].value; 
				break;
		   }
		}
		if (xvalue == 0) {
			alert('Please select an upper limit for the first number or numerator.');
			return;
		}
	}

	var yvalue = 0;
	if (yNegFlag == "N") {
		for (var j=0; j<6; j++) {
		   if (document.yvalueform.yvalue[j].checked) {
				yvalue = document.yvalueform.yvalue[j].value; 
				break;
		   }
		}
		if (yvalue == 0) {
			alert('Please select an upper limit for the second number or denominator.');
			return;
		}
	}

    var xnvalue = 0;
	if (xNegFlag == "Y") {
		for (var i=0; i<6; i++) {
		   if (document.xvalueform.xnvalue[i].checked) {
				xnvalue = document.xvalueform.xnvalue[i].value; 
				break;
		   }
		}
		if (xnvalue == 0) {
			alert('Please select a limit for the first number or numerator.');
			return;
		}
	}
	
    var ynvalue = 0;
	if (yNegFlag == "Y") {
		for (var j=0; j<6; j++) {
		   if (document.yvalueform.ynvalue[j].checked) {
				ynvalue = document.yvalueform.ynvalue[j].value; 
				break;
		   }
		}
		if (ynvalue == 0) {
			alert('Please select a limit for the second number or denominator.');
			return;
		}
	}

    var qvalue = 0;
    for (var k=0; k<5; k++) {
	   if (document.qvalueform.qvalue[k].checked) {
		    qvalue = document.qvalueform.qvalue[k].value; 
			break;
	   }
	}
	if (qvalue == 0) {
		alert('Please select number of questions in worksheet.');
		return;
	}
	
	var pStr = "op=GENERATEWORKSHEET&operation=" + operation +
	           "&xvalue=" + xvalue + "&xnvalue=" + xnvalue +
	           "&yvalue=" + yvalue + "&ynvalue=" + ynvalue +
			   "&xnflag=" + xNegFlag + "&ynflag=" + yNegFlag +
	           "&qvalue=" + qvalue +
			   "&studentname=" + document.getElementById("studentname").value +
			   "&sheetdate=" + document.getElementById("sheetdate").value;

    if (operation == "DA" || operation == "DS" || operation == "DM") {
		if (document.yvalueform.fractionflag.checked) pStr = pStr + "&fractionflag=Y";
	}
    if (operation == "D") {
		if (document.getElementById("remainder").checked) pStr = pStr + "&remainder=Y";
	}
	xmlhttpPost("shwqstgenerate.php","GENERATEWORKSHEET",pStr);
}

function printWorkSheet(printid) {
	var url = "http://www.simplyhomework.com/print.php?printid=" + printid;
	window.open(url,"simplyhomework.com",'width=800,height=1000,toolbar=yes,location=yes,scrollbars=yes,resizable=yes');
}

function checkNegativeInclude1() {
	if (document.getElementById("includenegative1").value == "Y") {
	   document.getElementById("DIV_NEGATIVE1").style.display = "";
	   document.getElementById("DIV_POSITIVE1").style.display = "none";
	}
	else {
	   document.getElementById("DIV_NEGATIVE1").style.display = "none";
	   document.getElementById("DIV_POSITIVE1").style.display = "";
	}
}

function checkNegativeInclude2() {
	if (document.getElementById("includenegative2").value == "Y") {
	   document.getElementById("DIV_NEGATIVE2").style.display = "";
	   document.getElementById("DIV_POSITIVE2").style.display = "none";
	}
	else {
	   document.getElementById("DIV_NEGATIVE2").style.display = "none";
	   document.getElementById("DIV_POSITIVE2").style.display = "";
	}
}

function showStore() {
	document.getElementById("divdata").innerHTML = '<iframe src="http://astore.amazon.com/simplyhomework-20" width="100%" height="4000" frameborder="0" scrolling="no"></iframe>';

}