var _numsms = 1;
var _afzender = 1;
var _reclame = 1;
var _creditsperSMS = 1;
var _reclamelength = 40;
var _smslength = 0;
var _free = 0;
var _unicodeflag = 0;

function calcCredits()
{
	var sms = _creditsperSMS;
	if (_free == 0) {
		if (_reclame == 1) sms -= 0.2;
		if (_afzender == 2) sms += 0.5;
	}
	
	var cost = (_numsms*sms);
	
	document.getElementById('smscredits').value = "" + cost;
}

function setCredits(val, id) {
	document.getElementById('offer_big').innerHTML = val;
	$('#smsad').val(id);
}

function smsAfzender()
{
	for( i = 0; i < document.smsform.smsafzender.length; i++ ) {
		if( document.smsform.smsafzender[i].checked == true ) {
			val = document.smsform.smsafzender[i].value;
			break;
		}
	}

	_afzender = val;
	
	var smsafzenderextra = document.getElementById('smsafzenderextra'); 
	if (val==1) {
		smsafzenderextra.value='';
	} else{
		smsafzenderextra.focus();
	}
	
	smslength();
}

function checkafzender() {
    var re = /^[A-z]*$/;
    var afzender = document.getElementById('smsafzenderextra');
    var afzendertag = document.getElementById('smsafzenderextra_tag');
    if (!re.test(afzender.value)) {
        afzender.value = afzender.value.replace(/[^A-z]/g,"");
        afzendertag.style.visibility = '';
    }
    else
    {
    	afzendertag.style.visibility = 'hidden';
    }
}

function smsAd() {
	var check = document.getElementById('smsreclame');
	if (check.checked) {
		_reclame = 0;
	}
	else {
		_reclame = 1;
	}
	
	smslength();
}

function setSendButton() {
   var sendbutton = document.getElementById('sendbutton');
   var smsnummer = document.getElementById('smsnummer');
   var smsland = document.getElementById('smsland');
   var smstext = document.getElementById('smstext');
   var SMSTEXT = smstext.value;
   
   var cansend = true;
   
   if (SMSTEXT.length == 0) cansend = false;
   //if (smsnummer.value.length<9) cansend = false;
   //if (smsnummer.value.length>10) cansend = false;
   if (smsland.options[smsland.selectedIndex].value<1) cansend = false;
   
   if (cansend) {
	   sendbutton.style.color = '#f9295c';	   
	   sendbutton.disabled = false;
   }
   else {
	   sendbutton.style.color = 'darkgrey';
	   sendbutton.disabled = true;
   }	
}

function smslength()
{
	
   var unicodeFlag = 0;
   var extraChars = 0;
   var msgCount = 0;
   
   var smsText = document.getElementById('smstext');
   var smsInfo = document.getElementById('smsInfo');
   var tekensInfo = document.getElementById('tekensInfo');
   var SMSTEXT = smsText.value;

   for (var i = 0; (!unicodeFlag && (i < SMSTEXT.length)); i++) {
      if ((SMSTEXT.charAt(i) >= '0') && (SMSTEXT.charAt(i) <= '9')) {
      } else if ((SMSTEXT.charAt(i) >= 'A') && (SMSTEXT.charAt(i) <= 'Z')) {
      } else if ((SMSTEXT.charAt(i) >= 'a') && (SMSTEXT.charAt(i) <= 'z')) {
      } else if (SMSTEXT.charAt(i) == '@') {
      } else if (SMSTEXT.charCodeAt(i) == 0xA3) {
      } else if (SMSTEXT.charAt(i) == '$') {
      } else if (SMSTEXT.charCodeAt(i) == 0xA5) {
      } else if (SMSTEXT.charCodeAt(i) == 0xE8) {
      } else if (SMSTEXT.charCodeAt(i) == 0xE9) {
      } else if (SMSTEXT.charCodeAt(i) == 0xF9) {
      } else if (SMSTEXT.charCodeAt(i) == 0xEC) {
      } else if (SMSTEXT.charCodeAt(i) == 0xF2) {
      } else if (SMSTEXT.charCodeAt(i) == 0xC7) {
      } else if (SMSTEXT.charAt(i) == '\r') {
      } else if (SMSTEXT.charAt(i) == '\n') {
      } else if (SMSTEXT.charCodeAt(i) == 0xD8) {
      } else if (SMSTEXT.charCodeAt(i) == 0xF8) {
      } else if (SMSTEXT.charCodeAt(i) == 0xC5) {
      } else if (SMSTEXT.charCodeAt(i) == 0xE5) {
      } else if (SMSTEXT.charCodeAt(i) == 0x394) {
      } else if (SMSTEXT.charAt(i) == '_') {
      } else if (SMSTEXT.charCodeAt(i) == 0x3A6) {
      } else if (SMSTEXT.charCodeAt(i) == 0x393) {
      } else if (SMSTEXT.charCodeAt(i) == 0x39B) {
      } else if (SMSTEXT.charCodeAt(i) == 0x3A9) {
      } else if (SMSTEXT.charCodeAt(i) == 0x3A0) {
      } else if (SMSTEXT.charCodeAt(i) == 0x3A8) {
      } else if (SMSTEXT.charCodeAt(i) == 0x3A3) {
      } else if (SMSTEXT.charCodeAt(i) == 0x398) {
      } else if (SMSTEXT.charCodeAt(i) == 0x39E) {
      } else if (SMSTEXT.charCodeAt(i) == 0xC6) {
      } else if (SMSTEXT.charCodeAt(i) == 0xE6) {
      } else if (SMSTEXT.charCodeAt(i) == 0xDF) {
      } else if (SMSTEXT.charCodeAt(i) == 0xC9) {
      } else if (SMSTEXT.charAt(i) == ' ') {
      } else if (SMSTEXT.charAt(i) == '!') {
      } else if (SMSTEXT.charAt(i) == '\"') {
      } else if (SMSTEXT.charAt(i) == '#') {
      } else if (SMSTEXT.charCodeAt(i) == 0xA4) {
      } else if (SMSTEXT.charAt(i) == '%') {
      } else if (SMSTEXT.charAt(i) == '&') {
      } else if (SMSTEXT.charAt(i) == '\'') {
      } else if (SMSTEXT.charAt(i) == '(') {
      } else if (SMSTEXT.charAt(i) == ')') {
      } else if (SMSTEXT.charAt(i) == '*') {
      } else if (SMSTEXT.charAt(i) == '+') {
      } else if (SMSTEXT.charAt(i) == ',') {
      } else if (SMSTEXT.charAt(i) == '-') {
      } else if (SMSTEXT.charAt(i) == '.') {
      } else if (SMSTEXT.charAt(i) == '/') {
      } else if (SMSTEXT.charAt(i) == ':') {
      } else if (SMSTEXT.charAt(i) == ';') {
      } else if (SMSTEXT.charAt(i) == '<') {
      } else if (SMSTEXT.charAt(i) == '=') {
      } else if (SMSTEXT.charAt(i) == '>') {
      } else if (SMSTEXT.charAt(i) == '?') {
      } else if (SMSTEXT.charCodeAt(i) == 0xA1) {
      } else if (SMSTEXT.charCodeAt(i) == 0xC4) {
      } else if (SMSTEXT.charCodeAt(i) == 0xD6) {
      } else if (SMSTEXT.charCodeAt(i) == 0xD1) {
      } else if (SMSTEXT.charCodeAt(i) == 0xDC) {
      } else if (SMSTEXT.charCodeAt(i) == 0xA7) {
      } else if (SMSTEXT.charCodeAt(i) == 0xBF) {
      } else if (SMSTEXT.charCodeAt(i) == 0xE4) {
      } else if (SMSTEXT.charCodeAt(i) == 0xF6) {
      } else if (SMSTEXT.charCodeAt(i) == 0xF1) {
      } else if (SMSTEXT.charCodeAt(i) == 0xFC) {
      } else if (SMSTEXT.charCodeAt(i) == 0xE0) {
      } else if (SMSTEXT.charCodeAt(i) == 0x391) {
      } else if (SMSTEXT.charCodeAt(i) == 0x392) {
      } else if (SMSTEXT.charCodeAt(i) == 0x395) {
      } else if (SMSTEXT.charCodeAt(i) == 0x396) {
      } else if (SMSTEXT.charCodeAt(i) == 0x397) {
      } else if (SMSTEXT.charCodeAt(i) == 0x399) {
      } else if (SMSTEXT.charCodeAt(i) == 0x39A) {
      } else if (SMSTEXT.charCodeAt(i) == 0x39C) {
      } else if (SMSTEXT.charCodeAt(i) == 0x39D) {
      } else if (SMSTEXT.charCodeAt(i) == 0x39F) {
      } else if (SMSTEXT.charCodeAt(i) == 0x3A1) {
      } else if (SMSTEXT.charCodeAt(i) == 0x3A4) {
      } else if (SMSTEXT.charCodeAt(i) == 0x3A5) {
      } else if (SMSTEXT.charCodeAt(i) == 0x3A7) {
      } else if (SMSTEXT.charAt(i) == '^') {
         extraChars++;
      } else if (SMSTEXT.charAt(i) == '{') {
         extraChars++;
      } else if (SMSTEXT.charAt(i) == '}') {
         extraChars++;
      } else if (SMSTEXT.charAt(i) == '\\') {
         extraChars++;
      } else if (SMSTEXT.charAt(i) == '[') {
         extraChars++;
      } else if (SMSTEXT.charAt(i) == '~') {
         extraChars++;
      } else if (SMSTEXT.charAt(i) == ']') {
         extraChars++;
      } else if (SMSTEXT.charAt(i) == '|') {
         extraChars++;
      } else if (SMSTEXT.charCodeAt(i) == 0x20AC) {
         extraChars++;
      } else {
         unicodeFlag = 1;
      }
   }
   
   
   if (unicodeFlag) {
	  maxlength = 402;
	  _unicodeflag = 1;
	  
	  // Reclame aftrekken
	  if ((_reclame == 1) || (_free == 1)) 
	  {
		  maxlength = 160 - _reclamelength;
		  msgCount = SMSTEXT.length + _reclamelength;
	  }
	  else
	  {
		  msgCount = SMSTEXT.length;
	  }
      if (msgCount>maxlength) {
    	  if (_reclame == 1)
    	  {
    		  smsText.value = SMSTEXT.substring(0, (maxlength));
    		  msgCount = maxlenth;
    	  }
    	  else
    	  {
    		  smsText.value = SMSTEXT.substring(0, maxlength);
    		  msgCount = maxlenth;
    	  }
      }
      if (msgCount <= 70) {
         msgCount = 1;
      }
      else {
         msgCount += (67-1);
         msgCount -= (msgCount % 67);
         msgCount /= 67;
      }
      if ((_reclame == 1) || (_free == 1)) {
    	  smsInfo.value = "" + msgCount + "/1";
    	  tekensInfo.value = "" + (maxlength-SMSTEXT.length);
      } else {
    	  smsInfo.value = "" + msgCount + "/6";
    	  tekensInfo.value = "" + (maxlength-SMSTEXT.length);
      }
      _numsms = msgCount;
      _smslength = SMSTEXT.length;
   } 
   else {
	  maxlength = 918;
	  _unicodeflag = 0;
	  
	  // Reclame aftrekken
	  if ((_reclame == 1) || (_free == 1)) 
	  {
		  maxlength = 160 - _reclamelength;
	      msgCount = SMSTEXT.length + extraChars + _reclamelength;
	  }
	  else
	  {
	      msgCount = SMSTEXT.length + extraChars;
	  }

      if (msgCount>maxlength) {
    	  if ((_reclame == 1) || (_free == 1)) 
    	  {
    		  smsText.value = SMSTEXT.substring(0, (maxlength));
    		  msgCount = maxlength;
    	  }
    	  else
    	  {
    		  smsText.value = SMSTEXT.substring(0, maxlength);
    		  msgCount = maxlength;
    	  }
      }
      if (msgCount <= 160) {
         msgCount = 1;
      }
      else {
         msgCount += (153-1);
         msgCount -= (msgCount % 153);
         msgCount /= 153;
      }
      _numsms = msgCount;
      _smslength = SMSTEXT.length + extraChars;
      if ((_reclame == 1) || (_free == 1))  {
    	  smsInfo.value = "" + msgCount + "/1";
    	  tekensInfo.value = "" + (maxlength-(SMSTEXT.length + extraChars));
      } else {
    	  smsInfo.value = "" + msgCount + "/6";
    	  tekensInfo.value = "" + (maxlength-(SMSTEXT.length + extraChars));
      }
   }
   setSendButton();
   calcCredits();
}

function sendcheck() {
	
   var smsText = document.getElementById('smstext');
   var smsLand = document.getElementById('smsland');
   var smsNummer = document.getElementById('smsnummer');
   var smsafzender = document.getElementById('smsafzender');
   var smsafzenderextra = document.getElementById('smsafzenderextra');
   var smsreclame = document.getElementById('smsreclame');
//   var smsemail = document.getElementById('smsemailkopie');
   
	for( i = 0; i < document.smsform.smsafzender.length; i++ ) {
		if( document.smsform.smsafzender[i].checked == true ) {
			smsafzenderval = document.smsform.smsafzender[i].value;
			break;
		}
	}
   
   cansend = true;
   
   if (_smslength == 0) cansend = false;
   if ((_unicodeflag == 1) && (_smslength>402)) cansend = false;
   if ((_unicodeflag == 0) && (_smslength>918)) cansend = false;
   if (smsLand.options[smsLand.selectedIndex].value<1) cansend = false;
   if ((smsNummer.value == '') || ((smsafzenderval == 2) && (smsafzenderextra.value == '')) || ((smsafzenderval == 2) && (smsafzenderextra.length > 11))) {
	   cansend = false;
	   if (smsNummer.value == '') {
		   document.getElementById('errormessage').innerHTML = "Controleer de ontvanger";
		   document.getElementById('smsnummer').focus();
	   } else {
		   document.getElementById('errormessage').innerHTML = "Controleer de afzender";
		   document.getElementById('smsafzenderextra').focus();
	   }
   }
   
   if (cansend) {
	   document.getElementById('smsform').submit();
   } else {
	   document.getElementById('errormessage').style.display= '';
   }
}

function selectad(id, text) {
	sms = document.getElementById('sms-preview');
	tekst = document.getElementById('smstext');
	document.getElementById('ad').value = id;
	
	sms.innerHTML = tekst.value + text;
	document.getElementById('submit_button').disabled = '';
}

function landselect() {
	smsprefix = document.getElementById('smsprefix');
	
	smsprefix.value = "+" + selval('smsland');
	if ( typeof( window[ '_creditsperSMS' ] ) != "undefined" ) {
		_creditsperSMS = credits[selval('smsland')];
		calcCredits();
	}
}

function selectcontact(land, nummer) {
	smsnummer = document.getElementById('smsnummer');
	smsland = document.getElementById('smsland');
	smsprefix = document.getElementById('smsprefix');
	
	smsprefix.value = "+" + land;
	smsnummer.value = nummer;

	for(var i=0;i<smsland.length;i++){
		if (smsland.options[i].value == land)
		{
			smsland.options[i].selected = true;
		}
	}
	landselect();
}

function setCookie(c_name,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value + '; path=/';
}

function selval(id) {
	return document.getElementById(id).options[document.getElementById(id).selectedIndex].value
}
